﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Net;
using System.Windows.Forms;


namespace littleAdminHelper
{
    class Mail
    {
        // For searching and Replacing Values in Mail Subject and Body
        private ArrayList searches = new ArrayList();
        private ArrayList replaces = new ArrayList();
        private static ArrayList mailcommands = new ArrayList();



        // General Mail Settings
        private String smtpServer;
        // Authentication
        private String smtpAuthUser;
        private String smtpAuthPassword;
        private Boolean SSLEnable = false;
        private Boolean smtpAuthRequired;




        private String senderaddress;
        private String mailSubject;
        private String mailText;


        // Attachement Settings
        private Boolean attachments_enabled = true;
        private ArrayList attachements = new ArrayList();

        public String getSenderAddress()
        {
            return this.senderaddress;
        }

        public String getSmtpAuthUser()
        {
            return this.smtpAuthUser;
        }

        public String getSmtpAuthPassword()
        {
            return this.smtpAuthPassword;
        }

        public Boolean getSSLEnable()
        {
            return this.SSLEnable;
        }

        public Boolean getSmtpAuthRequired()
        {
            return smtpAuthRequired;
        }

        public void setSmtpAuthUser(String authUser)
        {
            this.smtpAuthUser = authUser;
        }

        public void setSmtpAuthPassword(String password)
        {
            this.smtpAuthPassword = password;
        }

        public void setSSLEnable(Boolean enable)
        {
            this.SSLEnable = enable;
        }

        public void setSmtpAuthRequired(Boolean enable)
        {
            this.smtpAuthRequired = enable;
        }

        public void setSenderAddress(String senderaddress)
        {
            this.senderaddress = senderaddress;
        }

        public Mail() {
            
        }

        public Boolean getAttachements_enabled()
        {
            return attachments_enabled;
        }

        public void setAttachements_enabled(Boolean enabled)
        {
            this.attachments_enabled = enabled;
        }

        public void setMailSubject(String subject){
            this.mailSubject = subject;
        }

        public String getMailSubject()
        {
            return this.mailSubject;
        }

        public void setMailText(String mailText)
        {
            this.mailText = mailText;
        }

        public String getMailtext()
        {
            return this.mailText;
        }

        public void addAttachement(String path)
        {
            this.attachements.Add(path);
        }

        public void removeAttachement(int index)
        {
            this.attachements.Remove(index);
        }

        public ArrayList listAttachements()
        {
            return this.attachements;
        }
        
        public void sentmailMailto(String receipient){

            this.addReplace("\r\n", "%0A");

            if (this.attachements.Count.ToString() == "0" || this.attachments_enabled == false)
            {
               String text=this.replaceValues(this.getMailtext());
               Process.Start("mailto:" + receipient + "?subject=" + this.replaceValues(this.getMailSubject()) + "&body=" + text);

            }
            else
            {
                Process.Start("mailto:" + receipient + "?subject=" + this.getMailSubject() + "&body=" + this.getMailtext() +"&attachment=" + this.attachements[0].ToString());
            }

        }

        public void sentMailSMTP(String receipient){

            try
            {
                // create a new Mail Message
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(this.senderaddress, receipient);

                message.Subject = this.replaceValues(this.getMailSubject());
                message.Body = this.replaceValues(this.getMailtext());

                // adds Attachments
                if (this.attachments_enabled==true && this.attachements.Count.ToString()!="0"){

                    // adding all Attachements which have been written to Arralist attachments
                    for (int i = 0; i < int.Parse(this.attachements.Count.ToString()); i++)
                    {
                        message.Attachments.Add(new System.Net.Mail.Attachment(this.attachements[i].ToString()));
                    }
                }

                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
                smtp.Host = this.getMailserver();
                if (this.smtpAuthRequired)
                {
                    smtp.Credentials = new System.Net.NetworkCredential(this.smtpAuthUser, this.smtpAuthPassword);
                }

                if (this.SSLEnable)
                {
                    smtp.EnableSsl = this.SSLEnable;
                }

                smtp.Send(message);
            }
            catch (Exception e)
            {
                //MessageBox.Show("Connection to SMTP Server Failed please check your Mail Settings.");
                this.sentmailMailto(receipient);
            }

            

        }

        public void setMailserver(String mailServer)
        {
            this.smtpServer = mailServer;
        }

        public String getMailserver()
        {
            return this.smtpServer;
        }

        public void addReplace(String searchFor, String replaceWith)
        {
            this.replaces.Add(replaceWith);
            this.searches.Add(searchFor);
        }

        public void setReplaces(String[] searchFor, String[] replaceWith)
        {
            if (searchFor.Length == replaceWith.Length)
            {
                for (int i = 0; i < searchFor.Length; i++)
                {
                    this.addReplace(searchFor[i], replaceWith[i]);
                }
            }
            else
            {
                throw new ArrayTypeMismatchException("Array Size should be equal");
            }
        }

        private String replaceValues(String input)
        {
            if (input == null)
            {
                return null;
            }
            

                for (int i = 0; i < this.replaces.Count; i++)
                {
                    input = input.Replace(this.searches[i].ToString(), this.replaces[i].ToString());
                }


                //input = input.Replace("\r\n", Environment.NewLine);


            return input;
        }


    }
}
